package cate.common.util;

import cp.solution.business.game.component.NoticeParam;
import easy.java.practice.errorcode.EcResult;
import easy.java.util.StrTool;
import org.apache.commons.lang3.ArrayUtils;

public class GameResult<D> extends EcResult<D> {

    public GameResult() {}

    public GameResult(EcResult<D> er) {
        this.code = er.code;
        this.message = er.message;
        this.data = er.data;
    }

    public int contentId;

    public NoticeParam[] paramList;


    private void setContent(int contentId, NoticeParam... args) {
        this.contentId = contentId;
        if(args != null && args.length > 0) {
            paramList = args;
        }
    }

    @Override
    public GameResult<D> success() {
        return success("success");
    }

    @Override
    public GameResult<D> success(String message) {
        return success(message, null);
    }

    @Override
    public GameResult<D> success(String message, D data) {
        this.code = SUCCESS;
        this.message = message;
        if(data != null){
            this.data = data;
        }
        setContent(0, new NoticeParam(this.message));
        return this;
    }

    @Override
    public GameResult<D> fail() {
        return fail(0, "fail");
    }

    @Override
    public GameResult<D> fail(String message) {
        return fail(0, message);
    }

    @Override
    public GameResult<D> fail(Throwable t) {
        return fail(0, t.getMessage());
    }

    public GameResult<D> fail(GameResult<?> r){
        this.code = r.code;
        this.message = r.message;
        this.contentId = r.contentId;
        this.paramList = r.paramList;
        return this;
    }

    public GameResult<D> fail(EcResult<?> r) {
        this.code = r.code;
        this.message = r.message;
        return this;
    }

    public GameResult<D> fail(String defaultMessage, Object... args){
        return fail(0, defaultMessage, args);
    }

    public GameResult<D> fail(int contentId){
        return fail(contentId, "");
    }

    @Override
    public GameResult<D> fail(int contentId, String defaultMessage) {
        return fail(contentId, defaultMessage, NoticeParam.none);
    }

    public GameResult<D> fail(int contentId, String defaultMessage, NoticeParam... args){
        this.code = FAIL;
        defaultMessage = StrTool.format(defaultMessage, args);
        if(contentId == 0){
            setContent(0, new NoticeParam(defaultMessage));
        }else{
            setContent(contentId, args);
        }
        this.message = defaultMessage;
        return this;
    }

    public GameResult<D> fail(int contentId, String defaultMessage, Object... args){
        NoticeParam[] params = null;
        if(ArrayUtils.isNotEmpty(args)){
            params = new NoticeParam[args.length];
            for(int i=0; i<args.length; ++i){
                params[i] = new NoticeParam(args[i]);
            }
        }
        return fail(contentId, defaultMessage, params);
    }

    public GameResult<D> fail(int contentId, Object... args){
        return fail(contentId, "", args);
    }

    //============================= 由于上面的方法已经比较混乱了，下面提供整理过的方法 ===============================

    /** fail 只设置 code 和 message */
    public GameResult<D> f(int code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }

    /** fail 只设置 code 和 message */
    public GameResult<D> f(String message) {
        return f(EcResult.FAIL, message);
    }

    /** 只设置 内容 */
    public GameResult<D> c(int contentId, NoticeParam... args) {
        this.contentId = contentId;
        if(args != null && args.length > 0) {
            this.paramList = args;
        }
        return this;
    }

    /** 只设置 内容 */
    public GameResult<D> c(String content) {
        this.contentId = 0;
        this.paramList = new NoticeParam[] { new NoticeParam(content) };
        return this;
    }

    /** fail并且设置内容的最简易方式（快速开发临时用，因为这样子就不支持国际化了） */
    public GameResult<D> fc(String message) {
        this.code = EcResult.FAIL;
        this.message = message;
        return c(message);
    }

}
